<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" type="text/css" media="screen" href="../../css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="../../css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="../../layui/css/layui.css">
    <link rel="stylesheet" href="../../css/global.css" media="all"/>
</head>
<body>
<form class="layui-form" action="" style="margin-top: 10px">
    <div class="layui-form-item">
        <label class="layui-form-label">编号</label>
        <div class="layui-input-inline">
            <input type="text" name="dataKey" required lay-verify="required" placeholder="请输入" autocomplete="off"
                   class="layui-input" id="dataKey">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">值</label>
        <div class="layui-input-inline">
            <input type="text" name="dataValue" required lay-verify="required" placeholder="请输入" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script type="text/javascript" src="../../js/libs/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="../../js/jq.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../../js/plugin/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="../../js/my/permission.js"></script>
<script type="text/javascript" src="../../layui/layui.js"></script>
<script type="text/javascript" src="../../js/dict.js"></script>
<script type="text/javascript" src="../../js/main.js"></script>

<script>
    //Demo
    layui.use('form', function () {
        var form = layui.form;

        //获取url后的参数值
        function getUrlParam(key) {
            var href = window.location.href;
            var url = href.split("?");
            if (url.length <= 1) {
                return "";
            }
            var params = url[1].split("&");

            for (var i = 0; i < params.length; i++) {
                var param = params[i].split("=");
                if (key == param[0]) {
                    return param[1];
                }
            }
        }

        initData();

        function initData() {
            var dictId = getUrlParam("dictId");
            if (dictId != "") {
                $.ajax({
                    type: 'get',
                    url: '/dictDetails/findMaxDataKeyByDictId/' + dictId,
                    async: false,
                    success: function (data) {
                        $('#dataKey').val(data.data[0]);
                    }
                });
            }
        }

        //监听提交
        form.on('submit(formDemo)', function (data) {
            var dictId = getUrlParam("dictId");
            data.field.dictId = dictId;
            $.ajax({
                type: 'POST',
                dataType: 'json',
                url: '/dictDetails/add',
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(data.field),
                success: function (res) {
                    if (res.errorCode == 0) {
                        layer.msg("添加成功！", {time: 1000, icon: 1}, function () {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.location.reload();//刷新父页面，注意一定要在关闭当前iframe层之前执行刷新
                            parent.layer.close(index); //再执行关闭
                        });
                    } else {
                        layer.msg(res.errorMsg);
                    }
                }
            })
            return false;
        });
    });
</script>
</body>
</html>